$(document).ready(function(){
(function($){
    
	var initLayout = function() {
		var hash = window.location.hash.replace('#', '');
		var currentTab = $('ul.navigationTabs a')
							.bind('click', showTab)
							.filter('a[rel=' + hash + ']');
		if (currentTab.size() == 0) {
			currentTab = $('ul.navigationTabs a:first');
		}
		showTab.apply(currentTab.get(0));
		
		$('.inputDate').DatePicker({
			format:'m/d/Y',
			date: $('#inputDate').val(),
			current: $('#inputDate').val(),
			starts: 1,
			position: 'right',
			onBeforeShow: function(){
				$('#inputDate').DatePickerSetDate($('#inputDate').val(), true);
			},
			onChange: function(formated, dates){
				$('#inputDate').val(formated);
				if ($('#closeOnSelect input').attr('checked')) {
					$('#inputDate').DatePickerHide();
				}
			}
		});
		var now3 = new Date();
		now3.addDays(-4);
		var now4 = new Date()
        
		$('#widgetCalendar').DatePicker({
			flat: true,
			/*format: 'd B, Y',*/
            format: 'Ymd',
			date: [new Date(now3), new Date(now4)],
			calendars: 1,
			mode: 'range',
			starts: 1,
			onChange: function(formated) {
                
				/*$('#widgetField span').get(0).innerHTML = formated.join(' &divide; ');*/
                //$('#widgetField input').get(0).val() = formated.join('_');
                $('#DateFilter').val(formated.join('_'));
			}
		});
		var state = false;
		$('#widgetField>a').bind('click', function(){
			$('#widgetCalendar').stop().animate({height: state ? 0 : $('#widgetCalendar div.datepicker').get(0).offsetHeight}, 500);
			state = !state;
			return false;
		});
        $('#popClose').bind('click', function(){
            
			$('#widgetCalendar').stop().animate({height: state ? 0 : $('#widgetCalendar div.datepicker').get(0).offsetHeight}, 500);
			state = !state;
			return false;
		});
        $('#dateApply').bind('click', function(){
            
            
            if($('#DateFilter').val()=='')
            {
                var fromdate = new Date();
                fromdate.addDays(-4);
                var todate = new Date();
                var fdY = String(fromdate.getFullYear());
                var fdM = String(fromdate.getMonth()+1);
                var fdD = String(fromdate.getDate());

                if(fdD.length==1)
                {
                    fdD = '0'+fdD;
                }

                if(fdM.length==1)
                {
                    fdM = '0'+fdM;
                }

                var tdY = String(todate.getFullYear());
                var tdM = String(todate.getMonth()+1);
                var tdD = String(todate.getDate());

                if(tdM.length==1)
                {
                    tdM = '0'+tdM;
                }
                
                if(tdD.length==1)
                {
                    tdD = '0'+tdD;
                }

                var fromDateCurrent = fdY+fdM+fdD;
                var toDateCurrent = tdY+tdM+tdD;
                
                var dateCurrent = fromDateCurrent+'_'+toDateCurrent;
                $('#DateFilter').val(dateCurrent);
               
            }
            var location = $('#Location').val();
            dateFilter = $('#DateFilter').val();
			window.location=location+'?d='+dateFilter;
			return false;
		});

		$('#widgetCalendar div.datepicker').css('position', 'absolute');
	};
	
	var showTab = function(e) {
		var tabIndex = $('ul.navigationTabs a')
							.removeClass('active')
							.index(this);
		$(this)
			.addClass('active')
			.blur();
		$('div.tab')
			.hide()
				.eq(tabIndex)
				.show();
	};
	
	EYE.register(initLayout, 'init');
})(jQuery)
});